﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.Partner
{
    public class Company
    {
        public int CompanyID { get; set; }
        public string Name { get; set; }
        public string Alias { get; set; }
        public string Description { get; set; }
        public int FatherID { get; set; }
        public int Level { get; set; }
        public int Order { get; set; }
        public string TotalOrder { get; set; }
        public int ReturnValue { get; set; }

        public Company()
        {

        }

        ~Company()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Company theo ID
        /// </summary>
        /// <param name="CompanyID"></param>
        /// <returns></returns>
        public Company Get(int companyID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetInstanceSP<Company>("sp_Company_Select",
                new SqlParameter("@CompanyID", companyID));
        }

        /// <summary>
        /// Lấy giá trị Company theo ID
        /// </summary>
        /// <returns></returns>
        public Company Get()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetInstanceSP<Company>("sp_Company_Select",
                new SqlParameter("@CompanyID", CompanyID));
        }

        /// <summary>
        /// Thêm mới Company
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@Name", Name);
            pars[2] = new SqlParameter("@Alias", Alias);
            pars[3] = new SqlParameter("@Description", Description);
            pars[4] = new SqlParameter("@FatherID", FatherID);
            pars[5] = new SqlParameter("@Order", Order);

            db.ExecuteNonQuerySP("sp_Company_Insert", pars);
            CompanyID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Contract
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            SqlParameter[] pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@CompanyID", CompanyID);
            pars[2] = new SqlParameter("@Name", Name);
            pars[3] = new SqlParameter("@Alias", Alias);
            pars[4] = new SqlParameter("@Description", Description);
            pars[5] = new SqlParameter("@FatherID", FatherID);
            pars[6] = new SqlParameter("@Order", Order);

            db.ExecuteNonQuerySP("sp_Company_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        /// <param name="CompanyID"></param>
        public void Delete(int companyID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);

            db.ExecuteNonQuerySP("sp_Company_Delete",
                new SqlParameter("@CompanyID", companyID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);

            db.ExecuteNonQuerySP("sp_Company_Delete",
                new SqlParameter("@CompanyID", CompanyID));
        }

        public DataTable GetTList()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetDataTableSP("sp_Company_SelectList");
        }

        public List<Company> GetList()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetListSP<Company>("sp_Company_SelectList");
        }

        public List<Company> GetList(int fatherID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetListSP<Company>("sp_Company_SelectListSun",
                new SqlParameter("@FatherID", fatherID));
        }

        public DataTable GetTList(int fatherID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetDataTableSP("sp_Company_SelectListSun",
                new SqlParameter("@FatherID", fatherID));
        }
    }
}
